cc.Class({
    extends: cc.Component,
    properties: {
    },


    //弹窗
    gameHit(str){
        cc.loader.loadRes('prefabs/common/hit', function (err, prefab) {
            if (err) {
            }else{
                var curScene = cc.director.getScene();
                var node = cc.instantiate(prefab);
                var size = cc.winSize;
                if (curScene !== undefined && node !== undefined) {
                    node.position = cc.v2(size.width / 2, size.height / 2);
                    node.parent = curScene;
                    var script = node.getComponent("hit");
                    script.GameHitShow(str);
                }
            }
        });
    },

    saveData(){
        
    },

    //随机数
    random(lower,upper){
         return Math.floor(Math.random()*(upper-lower+1))+lower;
    },

    //深拷贝
    getType(obj){
        //tostring会返回对应不同的标签的构造函数
        var toString = Object.prototype.toString;
        var map = {
           '[object Boolean]'  : 'boolean', 
           '[object Number]'   : 'number', 
           '[object String]'   : 'string', 
           '[object Function]' : 'function', 
           '[object Array]'    : 'array', 
           '[object Date]'     : 'date', 
           '[object RegExp]'   : 'regExp', 
           '[object Undefined]': 'undefined',
           '[object Null]'     : 'null', 
           '[object Object]'   : 'object'
       };
       if(obj instanceof Element) {
            return 'element';
       }
       return map[toString.call(obj)];
    },

    deepClone(data){
        var type = this.getType(data);
        var obj;
        if(type === 'array'){
            obj = [];
        } else if(type === 'object'){
            obj = {};
        } else {
            //不再具有下一层次
            return data;
        }
        if(type === 'array'){
            for(var i = 0, len = data.length; i < len; i++){
                obj.push(this.deepClone(data[i]));
            }
        } else if(type === 'object'){
            for(var key in data){
                obj[key] = this.deepClone(data[key]);
            }
        }
        return obj;
    },

    FormatGold(Num) {
            if (Num >= 100000000) {
                var tempGold = Num;
                tempGold = Math.floor(Num/100000000)+"亿";
                var tmpY = Num-Math.floor(Num/100000000)*100000000
                if (tmpY >= 10000) {
                    tmpY = Math.floor(tmpY/10000)+"万";
                }
                if(tmpY==0){
                    tmpY="";
                }
                return tempGold + '' + tmpY
            }else{
                var tempGold = Num
                if (tempGold >= 10000) {
                    tempGold = tempGold / 10000
                    tempGold = tempGold.toFixed(2)      // 四舍五入 取后两位
                    tempGold = tempGold + "万"
                }
                return tempGold + ''
            }
    },

    //播放音效
    playBgMusic(filePath, loop) {
        var effectClip = cc.url.raw(filePath);
        var c_v = 0;
        var volume_yinxiao = cc.sys.localStorage.getItem('volume_yinxiao');
        if(volume_yinxiao==null){
            c_v = 0.7;
        }
        cc.audioEngine.play(effectClip, loop, c_v);
    },

    //数字变化效果
    updatenNumberAnim: function (curNum,yagoNum,labelNode,frontStr,behindStr) {
        var self = this;
    var difference = curNum - yagoNum;
    var absDifference = Math.abs(difference);
    var changeTimes = absDifference < 8 ? absDifference : 8;
    var changeUnit = absDifference < 8 ? 1 : Math.floor(difference / 8)
    for (let i = 0; i < changeTimes; i++) {
        (function (i) {
            setTimeout(() => {
                try {
                    if(labelNode){
                        if(labelNode!=undefined && labelNode!=null){
                            if(labelNode.name!=""){
                                labelNode.string = frontStr+(yagoNum += changeUnit)+behindStr
                                if (i == changeTimes - 1) {
                                    labelNode.string = frontStr+curNum+behindStr;
                                }else{
                                }
                            }
                        }
                    }
                } catch (e) {
                }
            }, 100*(i+1));
        })(i)
    }
    },  

    unButtonInteractable(node){
        node.interactable = false;
        this.scheduleOnce(function () {
            node.interactable = true;
        }, 0.7);
    },
});
